/***************************************************************************
 **
 **  Copyright (C) 2018 MaMinJie <canpool@163.com>
 **  Contact: https://github.com/canpool
 **
 **  This program is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see http://www.gnu.org/licenses/.
 **
****************************************************************************/
#include "qtqrcode.h"
#include "qrencode.h"
#include<QImage>
#include<QPainter>

QtQrcode::QtQrcode(QObject *parent)
    : QObject(parent)
{

}

QImage QtQrcode::createQRCodeTag(QStringList sl){
//    QStringList sl;
//    sl<<"设备名称：优盘"<<"设备编号：xxxx"<<"资产编号：xxx"<<"使用开始日期:2021-2-21 11111111111111111111111"<<"超期年限：12个月"<<"公司：深圳宝德计算机"  //"保养计划：月保养";
    QString text="["+sl.join(",")+"]";
    QString maxString=sl[0];
    for(auto i=sl.begin()+1;i!=sl.end();++i){
        if(maxString.length()<(*i).length()){
            maxString=(*i);
        }
    }
    QFont f("Microsoft YaHei",11);
    QFontMetrics fm(f);
    int maxWidth=fm.width(maxString);
    int height=126;
    QImage codeImage = createQRCodeImage(text,height);
    QImage resultImage(height+maxWidth+20+10,height+20,QImage::Format_RGB32);
    resultImage.fill(QColor(Qt::white).rgb());
    QPainter p(&resultImage);
    p.setFont(f);
    p.setPen(Qt::black);
    p.drawImage(10,10,codeImage);
    int i=0;
    for(QString ss: sl){
        ++i;
        p.drawText(codeImage.width()+10+10,height/(sl.length())*i*0.95+10,ss);
    }
    return resultImage;
}

QImage QtQrcode::createQRCodeImage(const QString &text, int height)
{
    int margin = 2;
    if (text.length() == 0)
    {
        return QImage();
    }
    QRcode *qrcode = QRcode_encodeString(text.toLocal8Bit(), 2, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (qrcode == NULL) {
        return QImage();
    }
    unsigned char *p, *q;
    p = NULL;
    q = NULL;
    int x, y, bit;
    int realwidth;

    realwidth = qrcode->width;
    QImage image = QImage(realwidth, realwidth, QImage::Format_Indexed8);
    QRgb value;
    value = qRgb(255,255,255);
    image.setColor(0, value);
    value = qRgb(0,0,0);
    image.setColor(1, value);
    image.setColor(2, value);
    image.fill(0);
    p = qrcode->data;
    for(y=0; y<qrcode->width; y++) {
        bit = 7;
        q += margin / 8;
        bit = 7 - (margin % 8);
        for(x=0; x<qrcode->width; x++) {
            if ((*p & 1) << bit)
                image.setPixel(x, y, 1);
            else
                image.setPixel(x, y, 0);
            bit--;
            if(bit < 0)
            {
                q++;
                bit = 7;
            }
            p++;
        }
    }
    return image.scaledToWidth(height);
}

QPixmap QtQrcode::createQRCode(const QString &text, int width)
{
    int margin = 2;
    if (text.length() == 0)
    {
        return QPixmap();
    }
    QRcode *qrcode = QRcode_encodeString(text.toLocal8Bit(), 2, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (qrcode == NULL) {
        return QPixmap();
    }
    unsigned char *p, *q;
    p = NULL;
    q = NULL;
    int x, y, bit;
    int realwidth;

    realwidth = qrcode->width;
    QImage image = QImage(realwidth, realwidth, QImage::Format_Indexed8);
    QRgb value;
    value = qRgb(255,255,255);
    image.setColor(0, value);
    value = qRgb(0,0,0);
    image.setColor(1, value);
    image.setColor(2, value);
    image.fill(0);
    p = qrcode->data;
    for(y=0; y<qrcode->width; y++) {
        bit = 7;
        q += margin / 8;
        bit = 7 - (margin % 8);
        for(x=0; x<qrcode->width; x++) {
            if ((*p & 1) << bit)
                image.setPixel(x, y, 1);
            else
                image.setPixel(x, y, 0);
            bit--;
            if(bit < 0)
            {
                q++;
                bit = 7;
            }
            p++;
        }
    }
    return QPixmap::fromImage(image.scaledToWidth(width));
}
